package com.jerry.passenger.driver.model;

import com.google.gson.Gson;
import com.jerry.passenger.MyApplication;
import com.jerry.passenger.account.bean.User;
import com.jerry.passenger.common.databus.RxBus;
import com.jerry.passenger.common.http.IHttpClient;
import com.jerry.passenger.common.http.IRequest;
import com.jerry.passenger.common.http.IResponse;
import com.jerry.passenger.common.http.api.API;
import com.jerry.passenger.common.http.biz.BaseBizResponse;
import com.jerry.passenger.common.http.impl.BaseRequest;
import com.jerry.passenger.common.lbs.bean.LocationInfo;
import com.jerry.passenger.common.storage.SPDao;
import com.jerry.passenger.common.util.LogUtil;
import com.jerry.passenger.driver.model.response.NearDriversResponse;
import com.jerry.passenger.order.model.response.OrderStateOptResponse;

import rx.functions.Func1;

public class DriverMangerImpl implements IDriverManager {
    private static final String TAG = "DriverMangerImpl";

    IHttpClient mHttpClient;

    public DriverMangerImpl(IHttpClient mHttpClient) {
        this.mHttpClient = mHttpClient;
    }

    @Override
    public void fetchNearDrivers(final double latitude, final double longitude) {

        RxBus.getInstance().chainProcess(new Func1() {
            @Override
            public Object call(Object o) {
                IRequest request = new BaseRequest(API.Config.getDomain() + API.GET_NEAR_DRIVERS);
                request.setBody("latitude", new Double(latitude).toString() );
                request.setBody("longitude", new Double(longitude).toString() );
                IResponse response = mHttpClient.get(request, false);
                if (response.getCode() == BaseBizResponse.STATE_OK) {
                    try {
                        NearDriversResponse nearDriversResponse =
                                new Gson().fromJson(response.getData(), NearDriversResponse.class);
                        return nearDriversResponse;
                    } catch (Exception e) {
                        return null;
                    }
                }
                return null;
            }
        });
    }

    @Override
    public void updateLocationToServer(final LocationInfo locationInfo) {
        RxBus.getInstance().chainProcess(new Func1() {
            @Override
            public Object call(Object o) {
                IRequest request = new BaseRequest(API.Config.getDomain() + API.UPLOAD_LOCATION);
                request.setBody("latitude", new Double(locationInfo.getLatitude()).toString() );
                request.setBody("longitude", new Double(locationInfo.getLongitude()).toString() );
                request.setBody("key",locationInfo.getKey());
                request.setBody("rotation", new Float(locationInfo.getRotation()).toString() );
                IResponse response = mHttpClient.post(request, false);
                if (response.getCode() == BaseBizResponse.STATE_OK) {
                    LogUtil.d(TAG, "位置上报成功");
                } else {
                    LogUtil.d(TAG, "位置上报失败");
                }
                return null;
            }
        });
    }

    /**
     * 呼叫司机
     * @param key
     * @param startLocation
     * @param endLocation
     */
    @Override
    public void callDriver(final String key,
                           final float cost,
                           final LocationInfo startLocation,
                           final LocationInfo endLocation) {
        RxBus.getInstance().chainProcess(new Func1() {
            @Override
            public Object call(Object o) {
                SPDao spDao = new SPDao(MyApplication.getInstance(), SPDao.FILE_ACCOUNT);
                User user = (User) spDao.get(SPDao.KEY_ACCOUNT, User.class);
                String uid = user.getUid();   //获取 uid
                String phone = user.getAccount();   //获取phone

                IRequest request = new BaseRequest(API.Config.getDomain() + API.CALL_DRIVER);
                request.setBody("key", key);
                request.setBody("uid",uid);
                request.setBody("phone", phone);
                request.setBody("startLatitude", new Double(startLocation.getLatitude()).toString() );
                request.setBody("startLongitude", new Double(startLocation.getLongitude()).toString() );
                request.setBody("endLatitude", new Double(endLocation.getLatitude()).toString() );
                request.setBody("endLongitude", new Double(endLocation.getLongitude()).toString() );
                request.setBody("cost", new Float(cost).toString());

                IResponse response = mHttpClient.post(request, false);
                OrderStateOptResponse orderStateOptResponse = new OrderStateOptResponse();
                if (response.getCode() == BaseBizResponse.STATE_OK) {
                    // 解析订单信息
                    orderStateOptResponse = new Gson().fromJson(response.getData(), OrderStateOptResponse.class);
                    orderStateOptResponse.setCode(response.getCode());
                    orderStateOptResponse.setState(OrderStateOptResponse.ORDER_STATE_CREATE);
                    LogUtil.d(TAG, "call driver: " + response.getData());

                    return orderStateOptResponse;
                }
                return null;
            }
        });
    }

}
